<style>
    .messagebox svg {
        width: 32px;
        height: 32px;
    }

    .messagebox textarea {
        height: 90px;
    }

    .messagebox pre {
        white-space: pre-wrap;
        word-break: break-word;
    }
</style>

<div class="container-fluid">
    <div class="row messagebox">
        <div class="col-md-12">
            <ul class="list-unstyled" id="messagelist"></ul>
        </div>
        <div class="col-md-12 my-3">
            <form id="form1">
                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text">昵称</span>
                    </div>
                    <input class="form-control" placeholder="输入昵称（可选）" name="UserNickname" maxlength="50">
                </div>
                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text">内容</span>
                    </div>
                    <textarea class="form-control" name="Content" placeholder=" 内容：写留言内容，文明点 ^_^ &#xD;&#xA; 支持 Markdown &#xD;&#xA; 快捷提交：Ctrl + Enter"></textarea>
                </div>
            </form>
            <button class="btn btn-warning" id="btnSave">留言</button>
        </div>
    </div>
</div>

@Html.Raw(Netnr.SharedApp.QuoteTo.Html("bmob.js,netnrmd.css,netnrmd.js,jdenticon.js"))

<script>
    ss.bmobInit();
    var mg = {
        page: 1,
        pageNumber: 999,
        bquery: Bmob.Query("NetnrMessage"),
        messageObjectSave: function () { return new mg.MessageObject() },
        list: function () {
            ss.loading();
            var query = mg.bquery;
            query.limit(mg.pageNumber);
            query.skip((mg.page - 1) * mg.pageNumber);
            query.find().then(res => {
                ss.loading(0);
                var htm = [];
                $(res).each(function (i) {
                    var id = 'mi' + (i + 1);
                    var nickname = ss.htmlEncode(this.UserNickname == "" ? "guest" : this.UserNickname);
                    htm.push('<li class="media mb-3 border-bottom" id="' + id + '">');
                    htm.push('<a class="mr-3" href="#' + id + '"><svg data-jdenticon-value="' + nickname + '"></svg></a>');
                    htm.push('<div class="media-body"><h6 class="mt-0 mb-2 text-info">' + nickname + '<small class="mx-3 text-muted">' + this.createdAt + '</small><a class="text-warning" href="#' + id + '">#' + (i + 1) + '</a></h6>');
                    if (this.IsBlock) {
                        htm.push('<em class="badge badge-secondary" title="该信息已被屏蔽">block</em>');
                    } else {
                        var vc = netnrmd.render(this.Content);
                        vc = vc.replace(/@@\S+/g, function (n) {
                            return '<span class="text-info">' + n + '</span>'
                        }).replace(/#\d+/g, function (n) {
                            return '<a href="' + n.replace("#", "#mi") + '" class="text-warning">' + n + '</a>'
                        });
                        htm.push('<div class="text-break">' + vc + '</div>');
                    }
                    htm.push('</div></li>');
                });
                if (htm.length) {
                    $('#messagelist').html(htm.join(''));
                } else {
                    $('#messagelist').html('no message');
                }

                jdenticon();
            }).catch(err => {
                jz.alert("查询失败");
                console.log(err);
                ss.loading(0);
            });
        }
    }

    mg.list();
    $('textarea[name="Content"]').keydown(function (e) {
        e = e || window.event;
        var keys = e.keyCode || e.which || e.charCode;
        if (keys == 13 && e.ctrlKey) {
            $('#btnSave')[0].click();
        }
    });
    $('#btnSave').click(function () {
        var un = $('input[name="UserNickname"]'), uc = $('textarea[name="Content"]'), objv = {};
        objv.UserNickname = un.val().trim();
        objv.Content = uc.val().substring(0, 9999);
        if (objv.Content == "") {
            jz.msg("请输入内容")
        } else {
            var query = mg.bquery;
            for (var i in objv) {
                query.set(i, objv[i]);
            }
            query.save().then(res => {
                jz.msg("操作成功");
                uc.val('');
                mg.list();
                ss.ls["nickname"] = objv.UserNickname;
                ss.lsSave();
            }).catch(err => {
                jz.alert("查询失败");
                console.log(err);
                ss.loading(0);
            });
        }
    });

    $('input[name="UserNickname"]').val(ss.lsStr("nickname"));
</script>